package com.leetcode.algorithm.y22.m07.w3;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 150. 逆波兰表达式求值
 * 
 * https://leetcode.cn/problems/evaluate-reverse-polish-notation/
 * 
 * @author jie.deng
 *
 */
class Question0150Solution01 {

	public int evalRPN(String[] tokens) {
		Deque<Integer> stack = new ArrayDeque<>();
		for (int i = 0; i < tokens.length; i++) {
			if ("+".equals(tokens[i]) || "-".equals(tokens[i]) || "*".equals(tokens[i]) || "/".equals(tokens[i])) {
				int a2 = stack.pop();
				int a1 = stack.pop();
				if ("+".equals(tokens[i])) {
					stack.push(a1 + a2);
				} else if ("-".equals(tokens[i])) {
					stack.push(a1 - a2);
				} else if ("*".equals(tokens[i])) {
					stack.push(a1 * a2);
				} else if ("/".equals(tokens[i])) {
					stack.push(a1 / a2);
				}
			} else {
				stack.push(Integer.parseInt(tokens[i]));
			}
		}
		return stack.pop();
	}

}